﻿using QuanLyGiaoVuDAL.ADOWrapper;
using QuanLyGiaoVuDAL.Mapper;
using QuanLyGiaoVuDAL.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyGiaoVuDAL.Repositories.DatabaseRepositories
{
    public class ChiTietThongBaoRepository:IChiTietThongBaoRepository
    {
        public ChiTietThongBaoDTO GetChiTietThongBaoById(Guid MaChiTietThongBao)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ChiTietThongBaoSelect]", DBParameterFactory.Create("@MaChiTietThongBao", MaChiTietThongBao));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToChiTietThongBaoDTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public IEnumerable<ChiTietThongBaoDTO> GetAllChiTietThongBao()
        {
            List<ChiTietThongBaoDTO> listChiTietThongBao = new List<ChiTietThongBaoDTO>();
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = DBParameterFactory.Create("MaChiTietThongBao", SqlDbType.UniqueIdentifier, DBNull.Value);
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ThongBaoSelect]", parameters);
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    listChiTietThongBao.Add(dataRow.ToChiTietThongBaoDTO());
                }
                return listChiTietThongBao.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public IEnumerable<ChiTietThongBaoDTO> GetChiTietThongBaoByNguoiNhan(Guid MaNguoiNhan)
        {
            List<ChiTietThongBaoDTO> listChiTietThongBao = new List<ChiTietThongBaoDTO>();
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = DBParameterFactory.Create("MaNguoiNhan", MaNguoiNhan);
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ChiTietThongBao_NguoiNhan_Select]", parameters);
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    listChiTietThongBao.Add(dataRow.ToChiTietThongBaoDTO());
                }
                return listChiTietThongBao.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public ChiTietThongBaoDTO InsertChiTietThongBao(ChiTietThongBaoDTO chiTietThongBaoDTO)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ChiTietThongBaoInsert]", GetParameterArray(chiTietThongBaoDTO));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToChiTietThongBaoDTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public ChiTietThongBaoDTO UPdateChiTietThongBao(ChiTietThongBaoDTO chiTietThongBaoDTO)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ChiTietThongBaoUpdate]", GetParameterArray(chiTietThongBaoDTO));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToChiTietThongBaoDTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool DeleteChiTietThongBao(Guid MaChiTietThongBao)
        {
            try
            {
                var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ChiTietThongBaoDelete]", DBParameterFactory.Create("MaChiTietThongBao", MaChiTietThongBao));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #region Private Methods
        private System.Data.SqlClient.SqlParameter[] GetParameterArray(ChiTietThongBaoDTO chiTietThongBaoDTO)
        {
            SqlParameter[] parameters = new SqlParameter[4];
            parameters[0] = DBParameterFactory.Create("MaChiTietThongBao", chiTietThongBaoDTO.MaChiTietThongBao);
            parameters[1] = DBParameterFactory.Create("MaThongBao", chiTietThongBaoDTO.MaThongBao);
            parameters[2] = DBParameterFactory.Create("MaNguoiNhan", chiTietThongBaoDTO.MaNguoiNhan);
            parameters[3] = DBParameterFactory.Create("DaXem", chiTietThongBaoDTO.DaXem);            
            return parameters;
        }
        #endregion
    }
}
